
/*
 *
 * Copyright (c) 2006 P.J.Leonard
 * 
 * http://www.frinika.com
 * 
 * This file is part of Frinika.
 * 
 * Frinika is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * Frinika is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Frinika; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package uk.ac.bath.domains.prey;

import uk.ac.bath.base.Cell;
import uk.ac.bath.base.Evaluator;
import uk.ac.bath.base.Space;
import uk.ac.bath.base.Fitness;
import uk.ac.bath.gui.View;

/**
 *
 * @author pjl
 */
public class PreyEvaluator extends Evaluator implements PreyViewable {


  
    HunterCell hunter;
    PreyCell prey;
   
    Space space;
    private int tickCount;
    Fitness fitness;
    
    public PreyEvaluator(PreySpace space) {
        this.space = space;
//        tweaks.add(initFit);
        tweaks.addAll(HunterCell.tweaks);
     
    }

    public Space getSpace() {
        return space;
    }

    @Override
    public Fitness tickImpl() {
//        if (resetRequested) {
//           hunter=null;
//           assert(false);
//           return;
//        }
//
        tickCount++;
        ((PreySpace) space).update(this);
        hunter.tick();
        prey.tick();
        
        if (hunter.fitness <= 0) {
            fitness.set(hunter.getAge());
        //    fitness.setDone(true);
        }
        return fitness;
    }

    public HunterCell getHunter() {
        return hunter;
    }

    public PreyCell getPrey() {
        return prey;
    }

//    @Override
//    public boolean endOfTrial() {
//       return hunter.getFitness() < 0;
//    }

   
//    @Override
//    public int getRunCount() {
//        return 1;
//    }

//    @Override
//    public Object getWorker() {
//        return hunter;
//    }

   

    

    @Override
    public void nextEvaluationImpl() {
        int cid = Cell.nextId();
        MyPoint p = ((PreySpace) space).home;

        String tag = "Hunter:" + cid;

        hunter= new HunterCell(currentMachine, p, tag);
        p = ((PreySpace) space).randomFreePoint();

        prey = new PreyCell(null,p, " PREY ");
        
        fitness = new Fitness(Float.MAX_VALUE,0.0f);
 
    }


    public int getTickCount() {
        return tickCount;
    }



    @Override
    public View createView() {
        return new PreyView(this);
    }

    @Override
    protected void initImpl() {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String reportSetup() {
//        throw new UnsupportedOperationException("Not supported yet.");
        return "! NOT IMPLEMERNTED YET";
    }


}
